package com.oron3.lock;

import java.util.List;

import com.oron3.locker.R;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SecretListAdapter extends ArrayAdapter<Secret>  {

	private boolean edit;
	public SecretListAdapter(final Context context, final int resource,
			final int textViewResourceId, final List<Secret> objects) {
		super(context, resource, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
	}

	public SecretListAdapter(final Context context, final int resource,
			final int textViewResourceId, final Secret[] objects) {
		super(context, resource, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
	}

	public SecretListAdapter(final Context context, final int resource,
			final int textViewResourceId) {
		super(context, resource, textViewResourceId);
		// TODO Auto-generated constructor stub
	}

	public SecretListAdapter(final Context context, final int resource, final List<Secret> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
	}

	public SecretListAdapter(final Context context, final int resource, final Secret[] objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
	}

	public SecretListAdapter(final Context context, final int resource) {
		super(context, resource);
		// TODO Auto-generated constructor stub
	}
	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		final Secret item = getItem(position);
		if (convertView == null) {
			final LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_item_event, null);
		}
		final TextView title = (TextView) convertView
				.findViewById(R.id.title);
		title.setText(item.title);
		final View delete = convertView.findViewById(R.id.delete);
		delete.setVisibility(edit ? View.VISIBLE : View.INVISIBLE);
		delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				final Intent intent = new Intent(LockDatabaseService.SECRET_REMOVE_REQUEST);
				intent.putExtra(LockDatabaseService.SECRET_EXTRA, item);
				LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
				remove(item);
				notifyDataSetChanged();
			}
		});
		return convertView;
	}



	public void setEdit(final boolean b) {
		edit=b;
		notifyDataSetChanged();
	}
}
